package com.atguigu.gmall.order.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 接收订单相关的消息实现类
 * 1：本次接收是为了取消订单
 */
@Component
@Slf4j
public class OrderReceiver {

    @Autowired
    private OrderInfoService orderInfoService;

    //接收消息/为了取消订单
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void cancelOrderMessage(String orderId, Message message, Channel channel) throws Exception {

        log.info("接收订单ID：" + orderId);
        //接收订单ID
        //System.out.println("接收订单ID：" + orderId);
        orderInfoService.cancelOrder(orderId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //接收消息   为了支付成功之后更新订单状态
    @RabbitListener(queues = MqConst.QUEUE_PAYMENT_PAY)
    public void updateOrderMessage(String orderId, Message message, Channel channel) throws Exception {
        log.info("接收订单ID：" + orderId);
        //接收订单ID
        orderInfoService.updateOrderStatus(orderId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
